#include "GWindow.h"
#include "Particle.h"


GWindow::GWindow(GameState *gamestate) : PhysicalObject(gamestate)
{
	addProperty("window");

	getBodyDef()->type = b2_staticBody;
	getBodyDef()->fixedRotation = true;
	b2FixtureDef *fixDef = new b2FixtureDef;
	b2PolygonShape *shape = new b2PolygonShape;
	setSize(b2Vec2(4.0f, 4.0f));
	shape->SetAsBox(0.5f, 1.8f);
	fixDef->shape = shape;
	fixDef->density = 1.0;
	fixDef->restitution = 0.3f;
	fixDef->isSensor = true;
	addFixtureDef(fixDef);
	setRotation(0.0f);

	TextureManager &tm = m_gameState->getTextureManager();
	m_sprite[0] = tm.getTexture("wn0.png");
	m_sprite[1] = tm.getTexture("wn1.png");

	m_frame = 0;
	m_collided = false;
	m_state = 0;
	m_level = 0;

	m_crashSoundBuffer.loadFromFile("./img/crash.wav");
	m_crashSound.setBuffer(m_crashSoundBuffer);
}


GWindow::~GWindow()
{
	
}

void GWindow::draw(){
	
	if(!m_visible)
		return;
	
	b2Vec2 position = getPosition();
	float rotation = m_body->GetAngle();
	b2Vec2 size = getSize();

	sf::Texture::bind(m_sprite[m_frame]);

	glColor4fv(m_color);
	glPushMatrix();
	{
		glTranslatef(position.x,position.y,0);
		glRotatef(rotation*57.2957795,0,0,1);
		glScalef(size.x, size.y, 1.0f);
		glBegin(GL_QUADS);
		{			
			glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f,  0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f,  0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f,  0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f,  0.0f);
		}
		glEnd();
	}
	glPopMatrix();

	sf::Texture::bind(NULL);
}

void GWindow::update(double dt){
	if(m_level == 1){

	}else if(m_gameState->getCurrentLevel() == 2){
		if(m_state == 0){
			setPosition(b2Vec2(getPosition().x+5*dt, getPosition().y));
			if(getPosition().x > 5)
				m_state = 1;
		}else{
			setPosition(b2Vec2(getPosition().x-5*dt, getPosition().y));
			if(getPosition().x < -2)
				m_state = 0;
		}
	}else if(m_gameState->getCurrentLevel() == 3){
		if(m_state == 0){
			setPosition(b2Vec2(getPosition().x, getPosition().y+5*dt));
			if(getPosition().y > 5)
				m_state = 1;
		}else{
			setPosition(b2Vec2(getPosition().x, getPosition().y-5*dt));
			if(getPosition().y < -2)
				m_state = 0;
		}
	}else if(m_gameState->getCurrentLevel() == 4){

	}else if(m_gameState->getCurrentLevel() == 5){
		if(m_state == 0){
			setPosition(b2Vec2(getPosition().x+5*dt, getPosition().y));
			if(getPosition().x > 5)
				m_state = 1;
		}else{
			setPosition(b2Vec2(getPosition().x-5*dt, getPosition().y));
			if(getPosition().x < -2)
				m_state = 0;
		}
	}else if(m_gameState->getCurrentLevel() == 6){
		if(m_state == 0){
			setPosition(b2Vec2(getPosition().x, getPosition().y+5*dt));
			if(getPosition().y > 5)
				m_state = 1;
		}else{
			setPosition(b2Vec2(getPosition().x, getPosition().y-5*dt));
			if(getPosition().y < -2)
				m_state = 0;
		}
	}

	if(m_collided){
		for(int i=0; i < 30; i++){
			Particle *particle = new Particle(m_gameState);
			float randX = (rand()%30)/10.0f-1.5;
			float randY = (rand()%30)/10.0f-1.5;
			float randU = (rand()%50)/10.0f;
			float randV = (rand()%50)/10.0f;
				
			particle->setPosition(b2Vec2(randX, randY)+this->getPosition());
			particle->setSize(b2Vec2((randU-randV)/5.0, (randV-randU)/5.0));
			m_gameState->addObject(particle);
			particle->getBody()->ApplyAngularImpulse(4.0+randV);
			particle->getBody()->SetLinearVelocity(b2Vec2(randX*15, randY*15));
		}

		m_collided = false;

		m_crashSound.play();

		
		m_gameState->m_levelCompleted = true;
		m_state = 0;
	}

}

void GWindow::beginContact(b2Contact *contact){
	PhysicalObject *me = (PhysicalObject*)contact->GetFixtureA()->GetBody()->GetUserData();
	PhysicalObject *other = (PhysicalObject*)contact->GetFixtureB()->GetBody()->GetUserData();

	if(me != this){ 
		PhysicalObject *temp;
		temp = me;
		me = other;
		other = temp;
	}

	if(other->hasProperty("player") && !m_gameState->m_levelCompleted){

		m_visible = false;
		m_collided = true;
		
	}
}